// pages/projectDetails/projectDetails.js
import api from "../../../utils/util.js";
var wxParse = require('../../../wxParse/wxParse.js'),
    app = getApp(),
    globalData = app.globalData;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navmess:{
      navtitle: "项目详情",
      backhide: 0,
      topheight: 1
    },
    boxHeight: 0,
    bottomGuide_bgheight_stauts: false, // false: height=>0%, true: height:=>100%
    swiperSetting:{
      indicatorDots: true,
      vertical: false,
      autoplay: false,
      interval: 2000,
      duration: 500,
    },
    demolist:{
      "urlList": [
        "./images/pic2.png",
        "./images/pic2.png",
      ]
    },
    shoppingPayInfo: {}, // 购物车的信息
    projectInfo: {}, //项目信息
    requestProjectInfo: {}, // 存放根据sign请求回来的项目信息
    token: wx.getStorageSync('token'),
    url: '',// 下一步跳转地址
    List: [],// 已选项目详细列表
    activeBeautician: '',// 已选美容师
    activeCommoditySum: '',// 项目选择总数量
    activeCommodityPricesSum: '',// 项目价格总数量
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var info = wx.getSystemInfoSync(),
        projectInfo = wx.getStorageSync('projectInfo');
    this.getProjectInfo(projectInfo);
    this.setData({boxHeight: info.screenHeight - 30});
    console.log(projectInfo);
  },
  // 用sign去请求项目详情
  getProjectInfo: function(obj){
    var projectInfo = obj.shoppingPayInfo,
        sign = obj.info.sign;
    this.setData({projectInfo: obj.info,shoppingPayInfo: obj.shoppingPayInfo});
    api.post('/mini/goods/projectDetail',{sign: sign}).then(res=>{
      let projectDetail = res.result.projectDetail; // 富文本内容
      console.log("数据回来了",projectDetail);
      let arr = 'demolist.urlList';
      this.setData({
        requestProjectInfo: res.result, 
        [arr]: res.result.urlList,
        List: projectInfo.List, // 已在购车里的项目
        activeBeautician: projectInfo.activeBeautician, // 已选美容师
        activeCommoditySum: projectInfo.activeCommoditySum, // 已选商品总数量
        activeCommodityPricesSum: projectInfo.activeCommodityPricesSum // 已选商品总价格
      });
      
      if(projectDetail!=null && projectDetail!=''){wxParse.wxParse('article', 'html',projectDetail, this);};
    })
  },
  // 加入购物车操作
  addtoCart: function(){
    if(this.Isproject(this.data.List)==true){
      this.toast('已在购车里了哦~');
      return;
    }
    this.data.projectInfo.number = this.data.projectInfo.number + 1;
    this.data.List.push(this.data.projectInfo);
    this.setData({projectInfo: this.data.projectInfo, List: this.data.List});
    globalData.list = this.data.projectInfo;// 修改全局变量触发 order.js 页面监听
    console.log("加入购车成功！",this.data.List);
    this.TotalCommodityPrices(this.data.List);
  },
  // 是否存在购车里了
  Isproject: function(List){
    var sign = this.data.projectInfo.sign; // 当前项目的唯一标识
    for(var i in List){
      if(List[i].sign == sign){return true;};
    }
    return false;
  },
  // 购物车的选项
  getCarOpction: function(e){
    let detail = e.detail;
    if(detail==0){
      globalData.list = {}; // 清空购物车
      this.data.projectInfo.number = 0;
      this.setData({List: [], activeCommoditySum: 0, activeCommodityPricesSum: 0});
      return;
    }else{
      globalData.list = detail.list; //触发监听
      this.TotalCommodityPrices(this.data.List);// 购物车变灰 
      if(detail.list.number != 0){return;}
      let List = this.data.List; // 拿到当前的List
      for(var i=0;i<List.length; i++){
        if(detail.list.id == List[i].id){List.splice(i,1); break;}
      }
      this.TotalCommodityPrices(this.data.List);// 购物车变灰 
      this.setData({List: List});
    }
    console.log("项目详情页",e)
  },
  // 已选择商品总数和价格
  TotalCommodityPrices: function(list){
    var List = JSON.parse(JSON.stringify(list)), number=0, Price=0,token=this.data.token;
      for(var key in List){
        number += List[key].number; // 商品数量
          Price += (token==''?List[key].originalPrice:List[key].projectDiscount) * List[key].number; // 未登录/会员价格
      }
      this.setData({activeCommoditySum: number, activeCommodityPricesSum: Price});
  },
  // 跳转美容师列表
  toIdx: function(){
    globalData.projectChange = ''; // 触发(order.js)里定义的方法 跳转美容师列表监听
    wx.navigateBack({delta: 1});
  },
  // 小吐司 -.-!!
  toast: function(title, icon){
    wx.showToast({title: title, icon: icon==undefined||icon==""?'none':icon})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取背景是否显示状态
  getBg_stauts: function(e){
    let status = e.detail;
    this.setData({bottomGuide_bgheight_stauts: status});
    console.log("status",status);
  }
})